/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using MyPhotoIndex.Interfaces;
using System.Xml;
using System.IO;
using MyPhotoIndex.Utilities.Types;
using MyPhotoIndex.Utilities;
using MyPhotoIndex.Interfaces.Tagging;
using MyPhotoIndex.Interfaces.Imaging;

namespace MyPhotoIndex.Plugins.WebCommunity
{
    internal class ImageDataCompiler
    {
        const string tag_ATTACHMENT_DATA = "attachmentdata";
        const string tag_IMAGE_DATA = "imagedata";
        const string tag_IMAGE_TAGS = "imagetags";
        const string tag_IMAGE_TAG = "imagetag";

        private IImageTagCollection m_tagsCollection;

        public ImageDataCompiler(IImageTagCollection tagsCollection)
        {
            m_tagsCollection = tagsCollection;
        }

        public string CompileOneImageDataPack(IImageData imageData)
        {
            StringWriter stringWriter = new  StringWriter();
            XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);

            xmlTextWriter.WriteStartElement(tag_ATTACHMENT_DATA);
            WriteGeneralImformation(imageData, xmlTextWriter);
            WriteTagInformation(imageData, xmlTextWriter);

            xmlTextWriter.WriteEndElement();

            return stringWriter.ToString();
        }

        private void WriteTagInformation(IImageData imageData, XmlTextWriter xmlTextWriter)
        {
            xmlTextWriter.WriteStartElement(tag_IMAGE_TAGS);
            foreach (int tagId in imageData.Tags)
            {
                WriteOneTagInformation(tagId, xmlTextWriter);
            }

            xmlTextWriter.WriteEndElement();
        }

        private void WriteOneTagInformation(int tagId, XmlTextWriter xmlTextWriter)
        {
            IImageTag imageTag = m_tagsCollection.GetTag(tagId);
            if (imageTag != null)
            {
                xmlTextWriter.WriteStartElement(tag_IMAGE_TAG);

                xmlTextWriter.WriteElementString("Id", imageTag.Id.ToString());
                xmlTextWriter.WriteElementString("Name", imageTag.Name);
                xmlTextWriter.WriteElementString("Description", imageTag.Description);

                xmlTextWriter.WriteEndElement();
            }
        }

        private void WriteGeneralImformation(IImageData imageData, XmlTextWriter xmlTextWriter)
        {
            xmlTextWriter.WriteStartElement(tag_IMAGE_DATA);

            xmlTextWriter.WriteElementString("Comment", imageData.Comment);
            xmlTextWriter.WriteElementString("FileName", imageData.FileName);
         
            xmlTextWriter.WriteStartElement("DateDigitized");
            xmlTextWriter.WriteValue(imageData.DateDigitized);
            xmlTextWriter.WriteEndElement ();

            xmlTextWriter.WriteStartElement("DateTaken");
            xmlTextWriter.WriteValue(imageData.DateTaken);
            xmlTextWriter.WriteEndElement();

            xmlTextWriter.WriteElementString("Id", imageData.Id.ToString());
            xmlTextWriter.WriteElementString("Rating", imageData.Rating.ToString());

            xmlTextWriter.WriteEndElement();
        }
    }
}
